package com.osce.go_score.controller;

import com.csce.domain.entity.basic.BasicExPlace;
import com.csce.domain.entity.go_score.GoScoreExam;
import com.csce.domain.entity.go_score.bo.GoScoreExamDtoIdsBo;
import com.csce.domain.entity.go_score.dto.GoScoreExamDto;
import com.csce.domain.entity.score.ScoreSheet;
import com.csce.domain.entity.score.ScoreSkill;
import com.doudou.core.lang.ObjUtil;
import com.doudou.core.str.StrUtil;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.entity.entity.SysUser;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.interfaces.basic.IBasicExPlaceService;
import com.osce.interfaces.go_score.IGoScoreExamService;
import com.osce.interfaces.score.IScoreSheetService;
import com.osce.interfaces.score.IScoreSkillService;
import com.osce.interfaces.system.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 妇产科考试Controller
 *
 * @author zzx
 * @since 2024-05-17
 */
@RestController
@RequestMapping("/go_score/go_score_exam")
public class GoScoreExamController extends BaseController {

    @Resource private IGoScoreExamService goScoreExamService;

    @Resource private IBasicExPlaceService basicExPlaceService;

    @Resource private IScoreSkillService scoreSkillService;
    @Resource private IScoreSheetService scoreSheetService;

    @Resource private ISysUserService sysUserService;

    /**
     * 查询妇产科考试列表
     */
    @PreAuthorize("@ss.hasPermi('go_score:go_score_exam:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoScoreExam goScoreExam) {
        startPage();
        List<GoScoreExam> list = goScoreExamService.selectGoScoreExamList(goScoreExam);
        return getDataTable(list);
    }

    /**
     * 导出妇产科考试列表
     */
    @PreAuthorize("@ss.hasPermi('go_score:go_score_exam:export')")
    @Log(title = "妇产科考试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoScoreExam goScoreExam) {
        List<GoScoreExam> list = goScoreExamService.selectGoScoreExamList(goScoreExam);
        ExcelUtil<GoScoreExam> util = new ExcelUtil<GoScoreExam>(GoScoreExam.class);
        util.exportExcel(response, list, "妇产科考试数据");
    }

    /**
     * 获取妇产科考试详细信息
     */
    @PreAuthorize("@ss.hasPermi('go_score:go_score_exam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(goScoreExamService.selectGoScoreExamById(id).convertGoScoreExamDto());
    }

    /**
     * 获取妇产科考试详细信息
     */
    @PreAuthorize("@ss.hasPermi('go_score:go_score_exam:query')")
    @GetMapping(value = "/all/{id}")
    public AjaxResult getInfoAll(@PathVariable("id") Long id) {
        GoScoreExamDto goScoreExamDto = goScoreExamService.selectGoScoreExamById(id).convertGoScoreExamDto();
        GoScoreExamDtoIdsBo allIds = goScoreExamDto.getAllIds();
        //房间列表
        List<BasicExPlace> basicExPlaces = basicExPlaceService.selectBasicExPlaceListByIds(allIds.getPlaceList());
        Map<Long, String> placeMap = basicExPlaces.stream().collect(Collectors.toMap(BasicExPlace::getId, BasicExPlace::getName));
        //学员列表
        List<SysUser> sysUsers = sysUserService.selectUserListByIds(allIds.getUserList());
        Map<Long, String> userMap = sysUsers.stream().filter(ObjUtil::isNotNull).collect(Collectors.toMap(SysUser::getUserId, o-> StrUtil.isNotBlank(o.getRemark()) ? o.getRemark() : o.getNickName()));
        //技能列表
        List<ScoreSkill> scoreSkills = scoreSkillService.selectScoreSkillListByIds(allIds.getSkillList());
        Map<Long, String> skillMap = scoreSkills.stream().collect(Collectors.toMap(ScoreSkill::getId, ScoreSkill::getName));
        //评分表编号
        List<ScoreSheet> scoreSheets = scoreSheetService.selectScoreSheetListByIds(allIds.getSheetList());
        Map<Long, String> sheetMap = scoreSheets.stream().collect(Collectors.toMap(ScoreSheet::getId, ScoreSheet::getName));
        return success(goScoreExamService.selectGoScoreExamById(id).convertGoScoreExamDto())
                .put("placeMap",placeMap)
                .put("skillMap",skillMap)
                .put("sheetMap",sheetMap)
                .put("userMap",userMap);
    }

    /**
     * 新增妇产科考试
     */
    @PreAuthorize("@ss.hasPermi('go_score:go_score_exam:add')")
    @Log(title = "妇产科考试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated GoScoreExamDto goScoreExam) {
        return toAjax(goScoreExamService.insertGoScoreExam(goScoreExam.convertGoScoreExam()));
    }

    /**
     * 修改妇产科考试
     */
    @PreAuthorize("@ss.hasPermi('go_score:go_score_exam:edit')")
    @Log(title = "妇产科考试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated GoScoreExamDto goScoreExam) {
        return toAjax(goScoreExamService.updateGoScoreExam(goScoreExam.convertGoScoreExam()));
    }

    /**
     * 删除妇产科考试
     */
    @PreAuthorize("@ss.hasPermi('go_score:go_score_exam:remove')")
    @Log(title = "妇产科考试", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(goScoreExamService.deleteGoScoreExamByIds(ids));
    }
}
